﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SHNOPData.SHNOPModel;
using SHNOPData.common;

namespace SHNOPData.ImportExcel
{
    public class ImportPinLv: IImportExcel
    {
        public void Import(string[,] array)
        {
            string retMsg = "";
            string errorMsg = "";
            int successCount = 0;
            List<PinLvInfo> listZd = GetPinLvInfoList(array, out errorMsg);
            int failCount = array.GetLength(0) - listZd.Count-1;
            foreach (var item in listZd)
            {
                bool isValidId = IsValidPlanID(item, ref retMsg);
                if (!isValidId)
                {
                    failCount++;
                    continue;
                }
                bool result = AppDBMng.ImportPinPv(item,ref retMsg);
                if (result)
                {
                    successCount += 1;
                }
                else
                {
                    failCount++;
                    retMsg = retMsg + "第" + item.No + "条记录导入失败\n";
                }
            }
            if (failCount == 0)
            {
                System.Web.HttpContext.Current.Session["batchImport"] = "成功导入" + successCount.ToString() + "条记录";
            }
            else
            {
                System.Web.HttpContext.Current.Session["batchImport"] = "成功导入" + successCount.ToString() + "条,失败" + failCount.ToString() + "条\n" + retMsg;
            }
           
        }
        private bool IsValidPlanID(PinLvInfo item, ref string retMsg)
        {
            if (item.GuihuaID2G.Length > 0)
            {
                item.GuihuaIDTD = null;
                return true;
            }
            else if (item.GuihuaID2G.Length == 0 && item.GuihuaIDTD.Length == 0)
            {
                retMsg = retMsg + "第" + item.No + "条记录导入失败,规划编号必须至少填写一个\n";
            }
            return false;
        }
        private  int Convert2Int(string info)
        {
            int outResult = 0;
            int rtn;
            rtn = info.Length == 0 ? 0 : int.TryParse(info, out outResult) ? outResult : 0;
            return rtn;
        }
        public List<PinLvInfo> GetPinLvInfoList(string[,] array, out string msg)
        {
            msg = "";
            List<PinLvInfo> pinLvList = new List<PinLvInfo>();
            if (array != null)
            {
                for (int i = 1; i < array.GetLength(0); i++)
                {
                    int j = 0;
                    while (j < array.GetLength(1))
                    {
                        try
                        {
                            PinLvInfo pinLv = new PinLvInfo();
                            #region 频率
                            pinLv.No = array[i, j++];
                            if (pinLv.No.Length == 36)
                            {
                                pinLv.pinLvID = new Guid(pinLv.No);
                            }
                            pinLv.company = array[i, j++];
                            pinLv.GuihuaID2G = array[i, j++];
                            pinLv.GuihuaIDTD = array[i, j++];
                            pinLv.isShenqing = 1;//array[i, j++].Trim() == "是" ? 1 : 0;//按照需求自动生成是
                            
                            pinLv.is900Shenqing = array[i, j++].Trim() == "是" ? 1 : 0;
                            pinLv.is900Fenpei = array[i, j++].Trim() == "是" ? 1 : 0;
                            pinLv._900Reason = array[i, j++];
                            pinLv._900GuihuaDate = pinLv.is900Shenqing==1 ? DateTime.Now.ToString() : null;
                            pinLv._900Gongdan =array[i, j++];

                            pinLv.is1800Shenqing = array[i, j++].Trim() == "是" ? 1 : 0;
                            pinLv.is1800Fenpei = array[i, j++].Trim()=="是"?1:0;
                            pinLv._1800Reason = array[i, j++];
                            pinLv._1800GuihuaDate = pinLv.is1800Shenqing== 1 ? DateTime.Now.ToString() : null;
                            pinLv._1800Gongdan = array[i, j++];

                            pinLv.isTDShengqing = array[i, j++].Trim() == "是" ? 1 : 0;
                            pinLv.isTDFenPei = array[i, j++].Trim() == "是" ? 1 : 0;
                            pinLv.TDReason = array[i, j++];
                            pinLv.TDGuihuaDate = pinLv.is1800Shenqing == 1 ? DateTime.Now.ToString() : null;
                            pinLv.TDGongdanNum = array[i, j++];
                            #endregion
                            pinLvList.Add(pinLv);
                        }
                        catch (System.Exception ex)
                        {
                        	//msg
                        }
                        
                    }

                }
            }
            return pinLvList;
        }
    }
}
